package com.nefu.editor.service.Impl;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.core.mapper.ArticleMapper;
import com.nefu.editor.core.mapper.OrderMapper;
import com.nefu.editor.core.model.Article;
import com.nefu.editor.core.model.Order;
import com.nefu.editor.core.model.User;
import com.nefu.editor.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author chenchenT
 * @date : 18-11-14
 * @since : java 8
 */
@Service
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;
    private final ArticleMapper articleMapper;

    @Autowired
    public OrderServiceImpl(OrderMapper orderMapper, ArticleMapper articleMapper) {
        this.orderMapper = orderMapper;
        this.articleMapper = articleMapper;
    }

    @Override
    public List<Map<String, Object>> getOrderByUserSystemId(User user) throws EdiException {
        List<Map<String, Object>> rtv = new ArrayList<>();
        if (null != user) {
            Order order = new Order();
            order.setUserSystemId(user.getSystemId());
            List<Order> orders = orderMapper.selectOrderByCondition(order);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (null != orders) {
                for (Order order1 : orders) {
                    Map<String, Object> map = new HashMap<>(6);
                    map.put("systemId", order1.getSystemId());
                    Article article = new Article();
                    article.setSystemId(order1.getArticleSystemId());
                    List<Article> articles = articleMapper.selectArticleByCondition(article);
                    if (null != articles && 1 == articles.size()) {
                        map.put("title", articles.get(0).getTitle());
                    } else {
                        map.put("title", null);
                    }
                    map.put("sum", order1.getSum());
                    map.put("status", order1.getStatus());
                    if (null != order1.getTime()) {
                        map.put("time", formatter.format(order1.getTime()));
                    } else {
                        map.put("time", null);
                    }
                    map.put("image", articles.get(0).getImage());
                    rtv.add(map);
                }
            } else {
                throw new EdiException("订单列表为空!");
            }
        } else {
            throw new EdiException("参数有误!");
        }
        return rtv;
    }

    @Override
    public List<Map<String, Object>> getOrderByWriterSystemId(Order order) throws EdiException {
        List<Map<String, Object>> rtv = new ArrayList<>();
        if (null != order && null != order.getWriterSystemId()) {
            List<Order> orders = orderMapper.selectOrderByCondition(order);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (null != orders && 0 < orders.size()) {
                for (Order order1 : orders) {
                    Map<String, Object> map = new HashMap<>(6);
                    map.put("systemId", order1.getSystemId());
                    Article article = new Article();
                    article.setSystemId(order1.getArticleSystemId());
                    List<Article> articles = articleMapper.selectArticleByCondition(article);
                    if (null != articles && 1 == articles.size()) {
                        map.put("title", articles.get(0).getTitle());
                    } else {
                        map.put("title", null);
                    }
                    if (null != order1.getTime()) {
                        map.put("time", formatter.format(order1.getTime()));
                    } else {
                        map.put("time", null);
                    }
                    map.put("status", order1.getStatus());
                    map.put("paid", order.getPaid());
                    map.put("progress", order.getProgress());
                    rtv.add(map);
                }
            } else {
                throw new EdiException("任务列表为空!");
            }
        } else {
            throw new EdiException("参数有误!");
        }
        return rtv;
    }

    @Override
    public Map<String, Object> getOrderDetailByOrderSystemId(Order order) throws EdiException {
        Map<String, Object> map = new HashMap<>(6);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (null != order && null != order.getSystemId()) {
            List<Order> orders = orderMapper.selectOrderByCondition(order);
            if (null != orders && 1 == orders.size()) {
                for (Order order1 : orders) {
                    map.put("systemId", order1.getSystemId());
                    Article article = new Article();
                    article.setSystemId(order1.getArticleSystemId());
                    List<Article> articles = articleMapper.selectArticleByCondition(article);
                    if (null != articles && 1 == articles.size()) {
                        map.put("title", articles.get(0).getTitle());
                    } else {
                        map.put("title", null);
                    }
                    if (null != order1.getTime()) {
                        map.put("time", formatter.format(order1.getTime()));
                    } else {
                        map.put("time", null);
                    }
                    map.put("status", order1.getStatus());
                    map.put("limitDay", order1.getLimitDay());
                    map.put("deadline", order.getDeadLine());
                    map.put("wordCount", article.getWordCount());
                    map.put("totalPay", order1.getTotalPay());
                    map.put("paid", order.getPaid());
                    map.put("progress", order.getProgress());
                }
            }
        } else {
            throw new EdiException("参数有误!");
        }
        return map;
    }

    @Override
    public Map<String, Object> getIncomeByWriterSystemId(Order order) throws EdiException {
        Map<String, Object> map1 = new HashMap<>(3);
        if (null != order && null != order.getWriterSystemId()) {
            int sum = 0;
            order.setStatus(1);
            List<Map<String, Object>> rtv = new ArrayList<>();
            List<Order> orders = orderMapper.selectOrderByCondition(order);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (null != orders && 0 < orders.size()) {
                for (Order order1 : orders) {
                    Map<String, Object> map = new LinkedHashMap<>(4);
                    map.put("systemId", order1.getSystemId());
                    if (null != order1.getTime()) {
                        map.put("time", formatter.format(order1.getTime()));
                    } else {
                        map.put("time", null);
                    }
                    Article article = new Article();
                    article.setSystemId(order1.getArticleSystemId());
                    List<Article> articles = articleMapper.selectArticleByCondition(article);
                    if (null != articles && 1 == articles.size()) {
                        map.put("title", articles.get(0).getTitle());
                    } else {
                        map.put("title", null);
                    }
                    map.put("paid", order1.getPaid());
                    sum += order1.getPaid();
                    rtv.add(map);
                }
                map1.put("systemId", order.getWriterSystemId());
                map1.put("orders", rtv);
                map1.put("sum", sum);
                return map1;
            }
        } else {
            throw new EdiException("参数有误!");
        }
        return map1;
    }
}